// ==================================================================
// 描 述：
// 作 者：王征:D
// 创建时间：2019/05/15 14:15:56
// ==============================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
using Yunchang;

namespace AutoChess
{
    public class ACGroupItem : MonoBehaviour
    {
        public GroupSpriteKit GroupSprite;
        public Text ChessNumTxt;
        public Slider ProgressSlider;
        public GameObject[] SliderTypes;
        public GameObject MarkItem;
        public RectTransform FlyEffectTrans;

        private int curActiveChessNum;
        private int activeMinChessNum;
        private int maxChessNum;
        private int recordLastChessNum;
        private ACGroupPanel parentPanel;
        private Table.DataModel.TableAutoChessGroup tableGroup;
        private List<int> buffNeedChessNumList = new List<int>();
        private HashSet<int> activeChessHash = new HashSet<int>();

        public bool IsActive
        { get { return curActiveChessNum >= activeMinChessNum; } }
        public bool HasChess
        { get { return curActiveChessNum > 0; } }

        /// <summary>
        /// 初始化,只运行一次
        /// </summary>
        /// <param name="panel"></param>
        public void Init(ACGroupPanel panel, Table.DataModel.TableAutoChessGroup tableGroup) 
        {
            parentPanel = panel;
            this.tableGroup = tableGroup;

            var allBuffs = Table.Blo.AutoChessBlo.GetGroupBuffsByGroupId(tableGroup.acHeroGroup);
            maxChessNum = allBuffs.Last().needHeroNum;
            activeMinChessNum = allBuffs.First().needHeroNum;

            int lastNeedChessNum = -1;
            for (int i = 0; i < allBuffs.Count; i++)
            {
                if (allBuffs[i].needHeroNum != lastNeedChessNum)
                {
                    lastNeedChessNum = allBuffs[i].needHeroNum;
                    buffNeedChessNumList.Add(allBuffs[i].needHeroNum);
                }
            }
            ProgressSlider.maxValue = buffNeedChessNumList.Count;
            for (int i = 0; i < SliderTypes.Length; i++)
            {
                // warn: 只有1-4的格子有效...
                SliderTypes[i].SetActive((i + 2) == ProgressSlider.maxValue);
            }

            GroupSprite.SetSpriteName(tableGroup.altasName);
            SetSelectedState(false);
        }

        /// <summary>
        /// 置空
        /// </summary>
        public void ClearChess()
        {
            curActiveChessNum = 0;
            activeChessHash.Clear();
        }

        public void AddChess(int heroId)
        {
            if(!activeChessHash.Contains(heroId))
            {
                activeChessHash.Add(heroId);
                ++curActiveChessNum;
            }
        }

        public int GetCurChessNum()
        {
            return curActiveChessNum;
        }

        public void OnSelect()
        {
            parentPanel.OnSelect(tableGroup.acHeroGroup);
        }

        public void SetSelectedState(bool isSelected)
        {
            MarkItem.SetActive(isSelected);
        }

        public void UIRefresh(bool showNotActive)
        {
            if (showNotActive)
                gameObject.SetActive(HasChess);
            else
                gameObject.SetActive(IsActive);

            // Slider
            int curBuffNum = 0;
            for (int i = 0; i < buffNeedChessNumList.Count; i++)
            {
                if (buffNeedChessNumList[i] <= curActiveChessNum)
                    curBuffNum = i + 1;
                else
                    break;
            }
            ProgressSlider.value = curBuffNum;

            // ChessText
            int maxNum = maxChessNum;
            for (int i = 0; i < buffNeedChessNumList.Count; i++)
            {
                if (buffNeedChessNumList[i] > curActiveChessNum)
                {
                    maxNum = buffNeedChessNumList[i];
                    break;
                }
            }

            ChessNumTxt.text = "<color=#FFF800>" + curActiveChessNum + "</color>" + "/" + maxNum;
            
            if (AutoChessPlayerCtr.Ins.CurrentPlayer.IsSelfPlayer)
            {
                int activeNeedNum = 0;
                for (int i = 0; i < buffNeedChessNumList.Count; i++)
                {
                    if (curActiveChessNum == buffNeedChessNumList[i])
                        activeNeedNum = buffNeedChessNumList[i];
                }

                // 这里是为了保证只显示一次特效
                if (activeNeedNum != 0 && curActiveChessNum != recordLastChessNum && activeNeedNum > recordLastChessNum)
                {
                    AutoChessManager.Ins.ShotFireFlyEffect(FlyEffectTrans, this.transform as RectTransform, tableGroup.acHeroGroup);
                }
                recordLastChessNum = curActiveChessNum;
            }
        }
    }
}
